package com.youyun56.gps.android;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.youyun56.gps.android.common.BaseActovity;
import com.youyun56.gps.android.common.CommandFactory;
import com.youyun56.gps.android.http.HttpManager;
import com.youyun56.gps.android.http.IHttpListener;
import com.youyun56.gps.android.pojo.CarryStatus;
import com.youyun56.gps.android.service.LocationUploadService;

public class CarStatusActivity extends BaseActovity {

    public static Handler handler;
    private Button btnHearvy;
    private final static String tag = CarStatusActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new CarStatusHandler(this);
        setContentView(R.layout.carposition);
        btnHearvy = (Button) findViewById(R.id.btnHearvy);
        changeText();
        btnHearvy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Button btn = (Button) v;
                btn.setEnabled(false);
                String o = null;
                if (btn.getText().equals(getResources().getString(R.string.EMPTY))
                        || btn.getText().equals(getResources().getString(R.string.UNKNOWNED))) {
                    o = CommandFactory.changeCarHearvy(CarryStatus.EMPTY.name());
                } else if (btn.getText().equals(getResources().getString(R.string.GOOD))) {
                    o = CommandFactory.changeCarHearvy(CarryStatus.GOOD.name());
                }
                HttpManager.getInstance().excutePost(o, new IHttpListener() {

                    @Override
                    public void onBack(boolean bSuccess, String strData, int nResponseCode) {
                        if (bSuccess) {
                            try {
                                JSONObject o = new JSONObject(strData);
                                LocationUploadService.getInstance().getCar().setCarryStatus(o.getString("hearvy"));
                                changeText();
                                btn.setEnabled(true);
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                Log.e(tag, e.getMessage(), e);
                            }
                        }
                    }
                });
            }
        });
    }

    public void changeText() {
        TextView carryStatus = (TextView) findViewById(R.id.carryStatus);
        String carStatus = LocationUploadService.getInstance().getCar().getCarryStatus();
        if (carStatus.equals(CarryStatus.EMPTY.name())) {
            carryStatus.setText(getResources().getString(R.string.EMPTY));
            btnHearvy.setText(getResources().getString(R.string.GOOD));
        } else if (carStatus.equals(CarryStatus.GOOD.name())) {
            carryStatus.setText(getResources().getString(R.string.GOOD));
            btnHearvy.setText(getResources().getString(R.string.EMPTY));
        } else {
            carryStatus.setText(getResources().getString(R.string.UNKNOWNED));
            btnHearvy.setText(getResources().getString(R.string.GOOD));
        }
    }

    public Button getBtnHearvy() {
        return btnHearvy;
    }
}
